package modulesAPI.v2.composite;
import java.util.GregorianCalendar;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;

import person.Person;
import model.Model;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModTextField;

public class MPerson extends Module<Person> {

	public MPerson(Module<?> ownerModule, Person content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		createModules();
		this.addModules(name,surname,taxCode,address,phoneNumber,birthDate,fax,email);
	}
	public MPerson(Module<?> ownerModule,String tag,String description, ViewType viewType){
		this(ownerModule, new Person(Model.NO_ID,"","","","","","",""), tag, description, viewType);
		GregorianCalendar gc=new GregorianCalendar();
		gc.add(GregorianCalendar.YEAR, -18);
		this.setContent(new Person(Model.NO_ID,"","","","","","","",gc));
	}
	private void createModules(){
		this.createView();
		if (this.isOptionEnabled(VIEW_LESS))this.viewLess();
		else if(this.isOptionEnabled(VIEW_MORE_LESS))this.ViewMoreLess();		
	}
	private void createView(){
		this.taxCode=new ModTextField(this, this.getContent().getTaxCode(), "PERSON_TAX", "PERSON_TAX_DESC",this.getViewType());
		this.address=new ModTextField(this, this.getContent().getAddress(), "PERSON_ADDRESS", "PERSON_ADDRESS_DESC",this.getViewType());
		this.fax=new ModTextField(this, this.getContent().getFax(), "PERSON_FAX", "PERSON_FAX_DESC",this.getViewType());
		this.email=new ModTextField(this, this.getContent().getEmail(), "PERSON_EMAIL", "PERSON_EMAIL_DESC",this.getViewType());
		this.name=new ModTextField(this, this.getContent().getName(), "PERSON_NAME", "PERSON_NAME_DESC",this.getViewType());
		this.surname=new ModTextField(this, this.getContent().getSurname(), "PERSON_SURNAME", "PERSON_SURNAME_DESC",this.getViewType());
		this.phoneNumber=new ModTextField(this, this.getContent().getPhoneNumber(), "PERSON_PHONE", "PERSON_PHONE_DESC",this.getViewType());
		this.birthDate=new ModCalendar(this, this.getContent().getBirthDate(), "PERSON_BIRTH", "PERSON_BIRTH_DESC", this.getViewType());
	}
	private void viewLess(){
		this.fax.setVisible(false);
		this.email.setVisible(false);
		this.phoneNumber.setVisible(false);
		this.birthDate.setVisible(false);
	}
	private void ViewMoreLess(){
		this.taxCode.setVisible(false);
		this.address.setVisible(false);
		this.fax.setVisible(false);
		this.email.setVisible(false);
		this.phoneNumber.setVisible(false);
		this.birthDate.setVisible(false);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {	
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getName()+" "+this.getContent().getSurname(), 
				this.getContent().getTaxCode(),isSelected);
	}
	protected void beforeFireChange(Module<?>mod){
		this.setContent(new Person(
				this.getContent().getId(),
				name.getContent(),
				surname.getContent(),
				taxCode.getContent(),
				address.getContent(),
				phoneNumber.getContent(),
				fax.getContent(),
				email.getContent(),
				birthDate.getContent()
				));
	}
	@Override
	protected void afterFireChange(Module<?> mod) {	}
	public static final int VIEW_LESS=0;
	public static final int VIEW_MORE_LESS=1;
	private ModCalendar birthDate;
	private Module<String> name;
	private Module<String> surname;
	private Module<String> taxCode;
	private Module<String> address;
	private Module<String> phoneNumber;
	private Module<String> fax;
	private Module<String> email;
}
